package com.xuecheng.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import java.util.Collections;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: 令牌配置
 * @date 2023/1/19 21:07
 **/
@Configuration
public class TokenConfig {

    /**
     * 令牌签名使用的密钥，公钥，可以用环境变量优化
     */
    private static final String SIGNING_KEY = System.getenv("JWT_SIGNING_KEY");

    /**
     * 用于存储和解析访问令牌的对象。这里使用的是 JwtTokenStore，它支持JWT令牌
     */
    private TokenStore tokenStore;

    /**
     * 用于将OAuth2访问令牌转换为JWT格式，并在需要时进行签名和验证。
     */
    private JwtAccessTokenConverter accessTokenConverter;

    /**
     * 注入TokenStore
     * @param tokenStore TokenStore
     */
    @Autowired
    public void setTokenStore(TokenStore tokenStore) {
        this.tokenStore = tokenStore;
    }

    /**
     * 注入JwtAccessTokenConverter
     * @param accessTokenConverter JwtAccessTokenConverter
     */
    @Autowired
    public void setAccessTokenConverter(JwtAccessTokenConverter accessTokenConverter) {
        this.accessTokenConverter = accessTokenConverter;
    }

    /**
     * 令牌存储策略
     * @return TokenStore
     */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    /**
     * 令牌转换器，用于将OAuth2访问令牌转换为JWT格式，并在需要时进行签名和验证
     * @return JwtAccessTokenConverter
     */
    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(SIGNING_KEY);
        return converter;
    }

    /**
     * 令牌服务，用名称为authorizationServerTokenServicesCustom，好在隔壁依赖注入作为令牌管理服务
     * @return AuthorizationServerTokenServices
     */
    @Bean(name = "authorizationServerTokenServicesCustom")
    public AuthorizationServerTokenServices tokenService() {
        DefaultTokenServices service = new DefaultTokenServices();
        service.setSupportRefreshToken(true);// 支持刷新令牌
        service.setTokenStore(tokenStore);// 令牌存储策略
        // 添加自定义信息到负载
        TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
        tokenEnhancerChain.setTokenEnhancers(Collections.singletonList(accessTokenConverter));
        service.setTokenEnhancer(tokenEnhancerChain);
        service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
        service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
        return service;
    }
}
